﻿using AjaxControlToolkit;
using ASP;
using IPMS.AccessAuth;
using IPMS.BLL;
using IPMS.Sort;
using IPMS.Common;
using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using System.Web;
using SysOperationLayer.DBHandle;
using System.Data;
using SysComm;

public partial class OptProductProjects : System.Web.UI.Page
{
    ProductDB m_productdb = new ProductDB();
    LogDB m_logdb = new LogDB();


    protected override void InitializeCulture()
    {
        string sCulture = ConfigurationManager.AppSettings["DefaultCulture"].ToString();
        try
        {
            sCulture = this.Session["Culture"].ToString();
        }
        catch (NullReferenceException)
        {
        }
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(sCulture);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(sCulture);
        base.InitializeCulture();
    }


    private void GetProductProjectList(string modelNo )
    {
        DataTable dt;
        dt = m_productdb.getProductProjectList(modelNo); ;
        gvProductProject.DataSource = dt;
        gvProductProject.DataBind();
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!SysUtil.IsAdministratorOrNormalUserType())
        {
            base.Response.Redirect("ErrorPage/NotAllowed.aspx");
            return;
        }

        if (!this.Page.IsPostBack)
        {
            ((Label)this.Master.FindControl("lblMstTitle")).Text = " | " + ISitemap.Products.ToString() + " | " + ISitemap.ProductProjects.ToString();
            string model;
            string modelName;
            model = this.Request["Model"].ToString();
            modelName = this.Request["ModelName"].ToString();

            this.txtModel.Text = model;
            this.txtModelName.Text = modelName;
            GetProductProjectList(model);
        }
    }


    protected void gvProductProject_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int ret;
        if (e.CommandName == "ForceDelete")
        {
            GridViewRow curRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
            ret = m_productdb.deleteProductProject(this.txtModel.Text, gvProductProject.DataKeys[curRow.RowIndex].Values["ProjectCode"].ToString());
            if (ret == 0)
            {
                m_logdb.writeSucLog(SysUtil.m_functiong_product, "delete product's project succeed!");
                Jscript.Alert("Delete Succeed!", this.Page);
                GetProductProjectList(this.txtModel.Text);
            }
            else
            {
                m_logdb.writeFaileLog(SysUtil.m_functiong_product, "delete product's project failed!");
                Jscript.Alert("Delete Failed!", this.Page);
            }
        }
    }

    protected void gvProductProject_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvProductProject.PageIndex = e.NewPageIndex;
        GetProductProjectList(this.txtModel.Text);
    }


    protected void btnAddProductProject_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("OptProductProjectsAdd.aspx?Model=" + this.txtModel.Text + "&ModelName=" + this.txtModelName.Text);
    }


    protected void btnBack_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("OptProduct.aspx");
    }
}
